package com.jxs.android.ges;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;

import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.weiget.BuLoadingDailog;
import com.jxs.android.weiget.RefreshListView;
import com.jxs.android.weiget.TopBar;

public abstract class MyCommnList extends AbstractActivity {
	protected RefreshListView refreshListView;
	protected LinearLayout bu_common_extends;

	boolean testFlag = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.bu_common_with_list);
		bu_common_extends = (LinearLayout) findViewById(R.id.bu_common_extends);
		refreshListView = (RefreshListView) findViewById(R.id.common_listview);
		refreshListView.setonRefreshListener(new RefreshListView.OnRefreshListener() {
			@Override
			public void onRefresh() {
				if (testFlag)
					test();
				else
					initListData(GesApplication.getUserInfo().getTicket());
			}
		});
		refreshListView.setAdapter(getAdapter(this));
		refreshListView.setOnItemClickListener(getItemClickListener());
		if (testFlag)
			test();
		else
			initListData(GesApplication.getUserInfo().getTicket());
	}

	@Deprecated
	protected void test() {

	}

	@Override
	public void topBarLogic(TopBar topBar) {
		super.topBarLogic(topBar);
		topBar.hindSpinnerBtn();
		topBar.setTitle(getTopBarTitle());
		topBarReturn(topBar);
	}

	public abstract String getMethod();

	public abstract AdapterView.OnItemClickListener getItemClickListener();

	public abstract Object paserData(SoapObject retMsgObject);

	public abstract void updateList(Object msgObj);

	public abstract BaseAdapter getAdapter(Context context);

	public abstract String getTopBarTitle();

	private void initListData(final String ticket) {//
		progressDialog = new BuLoadingDailog(this);
		progressDialog.setCancelable(false);
		progressDialog.showLoading(R.string.prompt_loading);
		new Thread(new Runnable() {
			@Override
			public void run() {
				String wsdl_url = GesApplication.WSDL_URL;// wsdl
				String namespace = GesApplication.WSDL_NAME_SPACE;// namespace
				String methodName = getMethod();// 要调用的方法名称
				SoapObject request = new SoapObject(namespace, methodName);
				request.addProperty("ticket", ticket);
				BuWebService webService = new BuWebService(wsdl_url, request);
				webService.setDoNet(false);
				webService.requestWSDL(new IWebService() {
					@Override
					public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
						SoapObject response = (SoapObject) envelope.getResponse();
						paser(response);
					}

					@Override
					public void error(String message, Exception e) {
						Message msg = new Message();
						msg.what = ERROR_MSG;
						msg.obj = message;
						handler.sendMessage(msg);
					}
				});
			}
		}).start();
	}

	private void paser(SoapObject soapObject) {
		String stauts = soapObject.getPropertySafelyAsString("status");
		SoapObject msgObjec = (SoapObject) soapObject.getProperty("msg");
		String message = msgObjec.getPropertySafelyAsString("message");
		if ("-2".equals(stauts)) {
			Message msg = new Message();
			msg.what = ERROR_MSG;
			msg.obj = message;
			handler.sendMessage(msg);
		} else if ("-1".equals(stauts)) {
			Message msg = new Message();
			msg.what = UNLOGIN;
			msg.obj = message;
			handler.sendMessage(msg);
		} else if ("0".equals(stauts)) {
			SoapObject taskBeanObject = (SoapObject) soapObject.getProperty("retMsg");
			if (null != taskBeanObject) {
				Message msg = new Message();
				msg.what = UPDATE_LIST;
				msg.obj = paserData(taskBeanObject);
				handler.sendMessage(msg);
			} else {
				Message msg = new Message();
				msg.what = ERROR_MSG;
				msg.obj = message;
				handler.sendMessage(msg);
			}
		}
	}

	protected int UPDATE_LIST = 0x331;
	private int ERROR_MSG = 0x332;
	private int UNLOGIN = 0x330;
	protected Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (null != progressDialog && progressDialog.isShowing()) {
				progressDialog.cancel();
				refreshListView.onRefreshComplete();
			}
			if (msg.what == UPDATE_LIST) {
				if (null != msg.obj) {
					updateList(msg.obj);
				}
			} else if (msg.what == ERROR_MSG) {
				errorMsgPop(msg.obj.toString());
			} else if (msg.what == UNLOGIN) {
				errorMsgToLogin(msg.obj.toString());
			}
		}
	};

}
